package com.shuoyue.ycgk.ui.mine.coupon;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.Coupon;
import com.shuoyue.ycgk.utils.NumberUtil;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.common.HintDialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券列表适配器
 */
public class CouponAdapter extends AppBaseQuickAdapter<Coupon> {
    int adapterType = 1;//用处1：选择使用，2.查看有效，3查看失效，4查看已使用

    public CouponAdapter(List data, int adapterType) {
        super(R.layout.item_coupon, data);
        this.adapterType = adapterType;
    }

    @Override
    protected void convert(BaseViewHolder helper, Coupon item) {
        helper.addOnClickListener(R.id.tv_explain);
        helper.addOnClickListener(R.id.lay_use);
        helper.addOnClickListener(R.id.lay_all);

        helper.setText(R.id.tv_title, item.getName());
        helper.setText(R.id.tv_validDay, TextUtils.isEmpty(item.getEffectiveEndTime()) ? "永久" : "有效期至:" + item.getEffectiveEndTime());
        helper.setText(R.id.tv_price, item.getType() == 1 ? ("￥" + item.getDiscountAmount()) : (NumberUtil.decimal1((100d - item.getDiscountRatio())/10,1) + "折"));
        helper.setText(R.id.tvisDiscount, item.getType() == 1 ? "满减券" : "折扣券");
        helper.setBackgroundRes(R.id.lay_price, adapterType == 1 || adapterType == 2
                ? R.mipmap.coupon_item_price_bg : R.mipmap.coupon_item_price_bg_inv);

//        helper.setGone(R.id.lay_use, adapterType != 2);//正在选择或者失效券显示

        String statueText = "";
        switch (adapterType) {
            case 1:
            case 2:
                statueText = "立即使用";
                helper.setTextColor(R.id.tv_use, ContextCompat.getColor(mContext, R.color.themeColor));
                break;
            case 3:
                statueText = "已失效";
                helper.setTextColor(R.id.tv_use, ContextCompat.getColor(mContext, R.color.text_sub_title));
                break;
            case 4:
                statueText = "已使用";
                helper.setTextColor(R.id.tv_use, ContextCompat.getColor(mContext, R.color.text_sub_title));
                break;
            default:
                helper.setTextColor(R.id.tv_use, ContextCompat.getColor(mContext, R.color.themeColor));
                break;
        }
        helper.setText(R.id.tv_use, statueText);

    }

    public List<Coupon> getSelectConpon() {
        List<Coupon> couponListBeans = new ArrayList<>();
        for (Coupon couponListBean : mData) {
            if (couponListBean.isSelect())
                couponListBeans.add(couponListBean);
        }
        return couponListBeans;
    }
}